﻿// JoeB - MyDictionary does not work with SoapFormatter, BUT Hashtable does!
// So this class isn't needed, and we can just use Hashtable.
#if not_needed

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace WebApplication
{
    /// <summary>
    /// Provides a serializable Hashtable replacement, and lets you specify the key and value types for better type checking.
    /// Use MyDictionary&lt;K,V&gt; in place of Hashtable. K is the type of key, V is the type of value in the hashtable.
    /// For example, instead of a HashTable X, where X[string] = Foo,
    /// use MyDictionary&lt;string,Foo&gt; table;
    /// 
    /// Simply use table[key] to access the dictionary. If needed, use table.D to access other Dictionary methods.
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    [Serializable]
	public class MyDictionary<TKey, TValue>
    {
        // we use a real Dictionary behind the scenes, but do not serialize it directly
        [XmlIgnore]
        public Dictionary<TKey, TValue> D = new Dictionary<TKey, TValue>();

        // DO NOT USE THIS DIRECTLY - it is here for serialization support only
        public MyEntry<TKey,TValue>[] _data
        {
            get
            {
                MyEntry<TKey, TValue>[] array = new MyEntry<TKey, TValue>[D.Count];
                int i = 0;
                foreach (KeyValuePair<TKey, TValue> pair in D)
                    array[i++] = new MyEntry<TKey, TValue>(pair.Key, pair.Value);
                return array;
            }
            set
            {
                D = new Dictionary<TKey, TValue>();
                for (int i = 0; i < value.Length; i++)
                    D.Add(value[i].Key, value[i].Value);
            }
        }

		// internal class for serializing the dictionary entries
		[Serializable]
		public class MyEntry<innerTKey, innerTValue>
		{
			public innerTKey Key;
			public innerTValue Value;

			public MyEntry() { }    // empty constructor for serialization

			public MyEntry(innerTKey key, innerTValue value)
			{
				Key = key;
				Value = value;
			}
		}

        // some helpers so callers can be ignorant of D
        public TValue this[TKey key]    // allow direct indexing on the class: redirects table[key] to table.D[key]
        {
            get { return D[key]; }
            set { D[key] = value; }
        }
        public int Count { get { return D.Count; } }
		public void Clear() { D.Clear(); }
		public void Add(TKey key, TValue value) { D[key] = value; }
		public bool ContainsKey(TKey key) { return D.ContainsKey(key); }

		// JoeB: I had to comment out this one since it causes a Reflect exception during serialize (write),
		// something about needing a default accessor on the KeyCollection. Maybe it needs to be genericized too?
		//public Dictionary<TKey,TValue>.KeyCollection Keys { get { return D.Keys; } }
	}
}

#endif // not_needed